package com.flycms.web.system.rest;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.flycms.core.entity.RestStatusCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.flycms.constant.Const;
import com.flycms.core.base.BaseController;
import com.flycms.core.entity.RestDataVo;
import com.flycms.core.utils.AdminSessionUtils;
import com.flycms.core.utils.web.utils.CommonUtils;
import com.flycms.module.admin.model.Admin;
import com.flycms.module.admin.service.AdminService;
import com.flycms.module.question.service.QuestionService;

/**
 * 开发公司：宏石<br/>
 * 版权：宏石<br/>
 *
 * @author sun-kaifei
 * @version 1.0 <br/>
 * @email xxx@qq.com
 * @Date: $time$ $date$
 */
@Controller
@RequestMapping("/system")
@Slf4j
public class IndexAdminController extends BaseController {

    //没有权限的跳转
    @GetMapping("/noLogin")
    @ResponseBody
    public RestDataVo noLogin() {
        RestDataVo dataVo = new RestDataVo();
        dataVo.setMsg("当前没有管理员登陆或登陆失效");
        dataVo.setCode(403);
        return dataVo;
    }

    /**
     * 前台JS读取管理员登录状态判断
     *
     * @throws Exception
     */
    @ResponseBody
    @GetMapping(value = "/admin/status")
    public RestDataVo userSession() throws Exception {
        Map<String,Object> data = new HashMap<>(16);
        if (getAdminUser() != null) {
            data.put("adminId",getAdminUser().getId());
            data.put("nickname",getAdminUser().getAdminName());
            String avatar = getAdminUser().getAvatar();
            if (avatar == null) {
                avatar = "/assets/skin/pc_theme/defalut/images/avatar/default.jpg";
            }
            data.put("avatar",avatar);
            return RestDataVo.OK("已有管理员登陆",data);
        } else {
            data.put("adminId","");
            data.put("nickname","");
            data.put("avatar","");
            return new RestDataVo("当前没有管理员登陆",RestStatusCode.FAIL,data);
        }
    }

    @ResponseBody
    @PostMapping(value = "/login_act")
    public RestDataVo userLogin(@RequestBody Map<String, Object> params) {
        try {
            String adminName = CommonUtils.transToString(params.get("adminName"));
            String password = CommonUtils.transToString(params.get("password"));
            String captcha = CommonUtils.transToString(params.get("captcha"));
            String kaptcha = (String) session.getAttribute("kaptcha");
            if (StringUtils.isBlank(adminName)) {
                return RestDataVo.ERROR("用户名不能为空");
            }
            if (StringUtils.isBlank(password)) {
                return RestDataVo.ERROR("密码不能为空");
            } else if (password.length() < 6 && password.length() > 30) {
                return RestDataVo.ERROR("密码最少6个字符，最多30个字符");
            }
            // 校验验证码
            if (captcha != null) {
                if (!captcha.equalsIgnoreCase(kaptcha)) {
                    return RestDataVo.ERROR("验证码错误");
                }
            }else{
                return RestDataVo.ERROR("验证码不能为空");
            }
            Admin entity = adminService.adminLogin(adminName,password,request);
            if(entity==null){
                return RestDataVo.ERROR("帐号或密码错误。");
            }else{
                session.removeAttribute(Const.KAPTCHA_SESSION_KEY);
                return RestDataVo.OK("操作成功",getAdminUser());
            }
        } catch (Exception e) {
            log.error("异常" + e);
            return RestDataVo.ERROR("登陆失败，异常：" + e.getMessage());
        }
    }

    /**
     * 管理员退出登录
     *
     */
    @GetMapping(value = "/logout")
    @ResponseBody
    public RestDataVo logout() {
        AdminSessionUtils.setLoginMember(request,null);
        return RestDataVo.OK("已退出登陆");
    }
}
